package jp.sourceforge.akjrcp.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;



public class SimpleFileList extends TableViewer{
	public static Log log=LogFactory.getLog(SimpleFileList.class);
	
	List fileList=new ArrayList();
	
	public SimpleFileList(Composite parent,int style){
		super(parent,style);
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
		this.getTable().setLinesVisible(true);
	        
	    		
	}
	
	  
	  

	  
	public SimpleFileList(Composite parent) {
		super(parent);
		
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
		
	}
	
	public void selectFile(int index){
		log.info("select:"+index);
		this.getTable().setSelection(index);
	}
	public void selectFile(File file){
		if(file==null){
			this.getTable().setSelection(-1);
			return;
		}
		File list[]=getFileList();
		for (int i = 0; i < list.length; i++) {
			if(list[i].getAbsolutePath().equals(file.getAbsolutePath())){
				this.getTable().setSelection(i);
				return;
			}
		}
	}
	
	public File popFile(){
		if(fileList.size()>0){
			Object obj=fileList.get(0);
			fileList.remove(obj);
			return (File)obj;
		}else{
			return null;
		}
	}

	
	
            class ViewContentProvider implements IStructuredContentProvider {
        		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
        			
        		}
        		public void dispose() {
        		}
        		public Object[] getElements(Object parent) {
        			
        			return fileList.toArray();
        		}
        	}
            
        	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
        		public String getColumnText(Object obj, int index) {
        			
        			return ((File)obj).getAbsolutePath();
        		}
        		public Image getColumnImage(Object obj, int index) {
        			return null;
        		}

        	}
        	

    public void removeFiles(){
    	fileList.removeAll(fileList);
    	refresh();
    }
	public void addFile(File file){
		fileList.add(file);
		refresh(true);
	}
	

	public int countFiles(){
		return fileList.size();
	}

	public File[] getFileList() {
		return (File[])fileList.toArray(new File[0]);
	}
	
	public class ItemControler {
		
	}
	

	
	
}
